package mta.command.merge;

import mta.io.LocalizationPackageWriter;
import mta.io.csv.writer.CsvTranslationWriterFactory;
import mta.io.csv.writer.CsvTranslationsWriter;
import mta.localization.LocalizationPackage;
import mta.localization.LocalizationPackageFactory;
import org.apache.commons.cli.ParseException;

/**
 * Magento Translation Assistant
 * Copyright (c) 2009 Dmitry Nikolaev
 * Contact author: blackliteon@gmail.com
 */
public class MergeBuilder {
    private String[] args;

    public MergeBuilder(String[] args) {
        this.args = args;
    }

    public Merge buildMergeCommand() {
        MergeBuilderOptions builderOptions = new MergeBuilderOptions(args);
        try {
            builderOptions.parse();
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }

        String to = builderOptions.getTo();
        String from = builderOptions.getFrom();

        LocalizationPackageFactory lFactory = new LocalizationPackageFactory();

        LocalizationPackage dest = lFactory.build(to);
        LocalizationPackage applied = lFactory.build(from);
        CsvTranslationWriterFactory writerFactory = new CsvTranslationWriterFactory();
        CsvTranslationsWriter mWriter = new CsvTranslationsWriter(to, writerFactory);
        LocalizationPackageWriter lWriter = new LocalizationPackageWriter(mWriter, true);

        return new Merge(dest, applied, lWriter);
    }
}
